﻿using SetSailCommon;
using SetSailDal;
using SetSailModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetSailView
{
    /// <summary>
    /// 存档记录
    /// </summary>
    public partial class Frm_CDJL : UserControl
    {
        public Frm_CDJL()
        {
            InitializeComponent();
        }
        private int pageIndex = 0;
        private void Frm_CDJL_Load(object sender, EventArgs e)
        {
            this.init(); //初始化
        }

        private void ucBtnExt1_BtnClick(object sender, EventArgs e)
        {
            //筛选条件
            pageIndex = 0; //清空index，防止数据变更触发分页事件
            this.ucPagerControl31.FirstPage(); //查询后跳转第一条

            var totalCount = 0;
            this.LoadList(out totalCount, getwhere());
            //设置分页 查询后数据变更需要重新设置分页
            this.setPage(totalCount);
        }


        private void init()
        {
            //设置每页条目
            this.ucPagerControl31.PageSize = Convert.ToInt32(CookieData.getInstance().ZDGL.Where(a => a.MXDM == "PAGECOUNT").FirstOrDefault().MXZ);
            //获取列表
            var totalCount = 0;
            this.LoadList(out totalCount);
            //设置分页
            this.setPage(totalCount);
        }

        /// <summary>
        /// 获取并绑定列表
        /// </summary>
        private void LoadList(out int totalCount, string where = "")
        {
            where += where.Length > 0 ? " AND " : "";
            where += $" CDDLX='存档'";
            ResultModel<List<QH_CDDJL>> result = CDDJLDal.GetPageList(this.ucPagerControl31.PageIndex, this.ucPagerControl31.PageSize, where);
            if (result.Code == ResultCode.Success)
            {
                totalCount = result.Count;
                this.dataGridView5.AutoGenerateColumns = false;
                this.dataGridView5.DataSource = result.Data;
            }
            else
            {
                totalCount = 0;
                MessShow.ShowMistake(result.Message);
            }
        }

        private string getwhere()
        {
            var name = txt_Name.InputText;
            var where = string.Empty;
            if (name.Length > 0)
            {
                where += $" CZZH like '%{name}%'";
            }
            return where;
        }

        private void setPage(int count)
        {
            List<object> lstPage2 = new List<object>();
            for (int i = 0; i < count; i++)
            {
                lstPage2.Add(i);
            }
            ucPagerControl31.DataSource = lstPage2;
        }

        /// <summary>
        /// 分页控件 数据操作事件
        /// </summary>
        /// <param name="currentSource"></param>
        private void ucPagerControl31_ShowSourceChanged(object currentSource)
        {
            if (currentSource != null)
            {
                var currindex = (Json.ToObject<int[]>(currentSource.ToJson()))[0];
                if (pageIndex != currindex)
                {
                    int count = 0;
                    LoadList(out count, getwhere());
                    pageIndex = currindex;
                }

            }
        }
    }
}
